﻿(function () {
    var localSettings = Windows.Storage.ApplicationData.current.localSettings;

    var storeData = function (data) {
        localSettings.values["sessionKey"] = data.sessonKey;
        localSettings.valueOf["username"] = data.username;
    };

    var clearData = function () {
        localSettings.values["sessionKey"] = "";
        localSettings.values["username"] = "";
    };

    var getSessionKey = function () {
        //return localSettings.values["sessionKey"];
        return '1oELcfCgbKuCkHMnRBeDBdhALCfQcCgKgylPgYpPSoTReXLBbW';
    };

    var getSessionKeyHeader = function () {
        var key = getSessionKey();
        return {
            "X-sessionKey": key,
            "Content-type": "application/json"
        };
    };

    var getDisplayName = function () {
        return localSettings.values["username"];
    };

    var MainPersister = WinJS.Class.define(function (url) {
        this._rootUrl = url;
        this.user = new UserPersister(this._rootUrl);
        this.event = new EventPersister(this._rootUrl);
    }, {
    });

    var UserPersister = WinJS.Class.define(function (url) {
        this.rootUrl = url + "users/";
    }, {
        displayName: function () {
            return getDisplayName();
        },
        login: function (user) {
            var url = this.rootUrl + 'login';
            var userData =
            {
                username: user.username,
                authCode: CryptoJS.SHA1(user.password).toString()
            };

            return HttpRequester.postJSON(url, userData).then(function (data) {
                storeData(data);
                return data;
            },
                function (err) {
                    return err;
                });
        },
        register: function (user) {
            var url = this.rootUrl + 'register';

            var userData =
            {
                username: user.username,
                authCode: CryptoJS.SHA1(user.password).toString(),
                mail: user.mail,
                phone: user.phone,
                image: user.image
            };
            return HttpRequester.postJSON(url, userData, getSessionKeyHeader()).then(function (data) {
                storeData(data);
                return data;
            }, function (err) {
                return err;
            });
        },
        logout: function () {
            var url = this.rootUrl + "logout";
            var headers = getSessionKeyHeader();
            return HttpRequester.putJSON(url, {}, headers).then(
                function () {
                    clearData();
                    return data;
                },
                function (err) {
                    return err;
                });

        },
        getFriends: function () {
            var url = this.rootUrl + "getFriends";
            var headers = getSessionKeyHeader();
            return HttpRequester.getJSON(url, headers);
        }
    });

    var EventPersister = WinJS.Class.define(function (url) {
        this.rootUrl = url + "events/";
    }, {
        getEvents: function () {
            var url = this.rootUrl + "getEvents";
            var headers = getSessionKeyHeader();
            return HttpRequester.getJson(url, headers);
        },
        createEvent: function (data) {
            var url = this.rootUrl + "createEvent";
            var headers = getSessionKeyHeader();
            return HttpRequester.postJSON(url, data, headers);
        }
    });

    var mainPersister;

    WinJS.Namespace.define("Persister", {
        getPersister: function () {
            return mainPersister || new MainPersister(ApiSharedInfo.apiRootUrl);
        }
    });
}());